///********************************************************************
///* Copyright © 2011 fishr (fishr.flash@gmail.com)  
///********************************************************************
package su.fishr.effects.prismavision.views 
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	
   /**
	 * ...
	 * @version                1.0
	 * @playerversion          Flash 9
	 * @langversion            3.0
	 * @author                 fishr
	 * @created                16.08.2011 3:01
	 * @since                  16.08.2011 3:01
	 */
	public  class PrismasHolder extends Sprite
	{
	
	/**-------------------------------------------------------------------------------
	* 
	*	   						V A R I A B L E ' S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
	    static public const AUTHOR:String = "fishr (fishr.flash@gmail.com)";	
		static public const END_TURN:String = "endTurn";
		private var _fwdSteps:Array;
		private var _backSteps:Array;
		private var _prismas:Array;
		private var _rows:Array;
		private var _asyn:int;
		private var _vertical:Boolean;
		private var _senseOfRotate:int;
		
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*	 						P R O P E R T I E S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		public function set fwdSteps(value:Array):void 
		{
			_fwdSteps = value;
		}
		
		public function set backSteps(value:Array):void 
		{
			_backSteps = value;
		}
		
		public function set asyn(value:int):void 
		{
			_asyn = value;
		}
		
	//}
	/**-------------------------------------------------------------------------------
	* 
	*								P U B L I C 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		public function PrismasHolder(arrFragments:Array, arrMasks:Array, corrBrightness:Number, factorShadow:Number)
		{
			super();
			
			init( arrFragments, arrMasks, corrBrightness, factorShadow );
		}
		
		public function createSecond(arrFragments:Array, fwdSteps:Array, backSteps:Array, asyn:int, vertical:Boolean, senseOfRotate:int , arrMasks:Array = null):void
		{
			_asyn = asyn;
			_vertical = vertical;
			_fwdSteps = fwdSteps;
			_backSteps = backSteps;
			_senseOfRotate = senseOfRotate;
			var index:int = 0;
			var arrMsk:Bitmap;
			
			for ( var j:int = 0; j < arrFragments.length; j++ )
			{
				for ( var i:int = 0; i < arrFragments[j].length; i++ )
				{
					if ( arrMasks )
					{
						arrMsk = arrMasks[j][i];
						arrMsk.x = arrMsk.y = 0;
					}
					arrFragments[j][i].x = arrFragments[j][i].y = 0;
					_prismas[index].secPict( arrFragments[j][i], arrMsk );
					index ++ ;
				}
			}

			
			 turnSignalHandler();
		}
		

		
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*								P R I V A T E 	
	* 
	* --------------------------------------------------------------------------------
	*/	
	//{
		
		private function init(arrFragments:Array, arrMasks:Array, corrBrightness:Number, factorShadow:Number ):void
		{
			addPrismas( arrFragments, arrMasks, corrBrightness, factorShadow );
		}
		
		private function addPrismas(arrFragments:Array, arrMasks:Array, corrBrightness:Number, factorShadow:Number):void
		{
			_prismas = new Array();
			var prisma:Prisma;
			_rows = new Array();
			var rowPrismas:Array;
			var rowsControll:RowsControll;
			var maskBitmap:Bitmap;
			
			
		    for ( var j:int = 0; j < arrFragments.length; j++ )
			{
				rowPrismas = new Array();
				
				for ( var i:int = 0; i < arrFragments[j].length; i++)
				{
					if ( arrMasks ) 
					{
						maskBitmap = arrMasks[j][i] as Bitmap;
						arrMasks[j][i].x = arrMasks[j][i].y = 0;
						prisma = new PrismaFilters( arrFragments[j][i] as Bitmap, maskBitmap );
						PrismaFilters(prisma).corrBrightness = corrBrightness;
						PrismaFilters(prisma).factorShadow = factorShadow;
						
					}
					else
					{
						prisma = new Prisma( arrFragments[j][i] as Bitmap );
					}
					
					prisma.x = arrFragments[j][i].x;
					prisma.y = arrFragments[j][i].y;
					arrFragments[j][i].x =  arrFragments[j][i].y = 0

					_prismas.push( prisma );
					rowPrismas.push( prisma );
					this.addChild( prisma );
				}
				
				if ( j / 2 is int === false ) rowPrismas.reverse();
				
				rowsControll = new RowsControll( rowPrismas );
				_rows.push( rowsControll );
				
				
			}
			
			
			
		}

		private function turnSignalHandler():void 
		{
			var prev:Array;
			var second:Array;
			
			switch( _senseOfRotate )
			{
				case 1:
					prev = _fwdSteps;
					second = _rows.length > 1? _backSteps:null;
					break;
				case 2:
					prev = _backSteps;
					second = _rows.length > 1? _fwdSteps:null;
					break;
				case 3:
					prev = _fwdSteps;
					second = _backSteps;
				case 4:
					prev = _backSteps;
					second = _fwdSteps ;
			}
			
			
			for ( var i:int = 0; i < _rows.length; i++ ) 
			{
				
				_rows[i].addEventListener( RowsControll.ROW_STOP, rowStopHandler);
				if ( i / 2 is int )
				{
					if ( _senseOfRotate < 3 )_rows[i].rotationRow(_asyn, _vertical, prev );
					else _rows[i].rotationRow(_asyn, _vertical, prev, second );
				}
				else
				{
					if ( _senseOfRotate < 3 )_rows[i].rotationRow(_asyn, _vertical, second );
					else _rows[i].rotationRow(_asyn, _vertical, second , prev);
				}
			}

		}
		
		private function rowStopHandler(e:Event):void 
		{
			e.target.removeEventListener( RowsControll.ROW_STOP, rowStopHandler);
			if ( e.target === _rows[ _rows.length - 1 ] ) this.dispatchEvent( new Event( END_TURN ) );
			
		}
		

	
	
	//}

	}

}